Public Partial Class FrmBancoABM
    Inherits System.Web.UI.Page
    Public Banco As CapaDeNegocios.nBanco = New CapaDeNegocios.nBanco
    Public Imputacion As CapaDeNegocios.nImputacion = New CapaDeNegocios.nImputacion

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            LLenarGridBanco()
            LlenarComboBanco()
            LblError.Visible = False
        End If
    End Sub
    Sub LlenarComboBanco()
        CboImputacion.DataSource = Imputacion.ListarImputacion(0, "", "")
        CboImputacion.DataTextField = "descripcionimp"
        CboImputacion.DataValueField = "CodigoImputacion"
        CboImputacion.DataBind()
    End Sub
    Protected Sub LinkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkGuardar.Click
        Dim Cont As Integer = 0
        LblError.Visible = False
        LblError.Text = ""
        If CboImputacion.Text <> 1 Then
            LblError.Text = "Error,solo puede dar de alta Bancos"
            Cont = Cont + 1
        End If
        If Len(TxtBanco.Text) < 1 Then
            LblError.Text = LblError.Text + "- Error, el Banco no contiene datos"
            Cont = Cont + 1
        End If
        If Cont = 0 Then
            If HFBanco.Value = "" Then
                If Len(Trim(TxtBanco.Text)) > 1 Then
                    If Not Banco.ExisteBanco(Trim(TxtBanco.Text)) Then
                        If Banco.InsertarBanco(Trim(TxtBanco.Text), CboImputacion.SelectedValue.ToString()) Then
                            Me.Response.Write("<script languaje='javascript'>window.alert('El Alta fue Exitosa')</script>")
                            LimpiarFormulario()
                        Else
                            LblError.Visible = True
                            LblError.Text = "Error, no se pudo realizar el Alta."
                        End If
                    Else
                        LblError.Visible = True
                        LblError.Text = "Error, el Banco ya existe."
                        
                    End If
                Else
                    LblError.Visible = True
                    LblError.Text = "Error, verifique los datos."
                End If
            Else
                If Len(Trim(TxtBanco.Text)) > 1 Then
                    If Not Banco.ExisteBanco(Trim(TxtBanco.Text)) Then
                        If Banco.ModificarBanco(Int(HFBanco.Value), Trim(TxtBanco.Text), CboImputacion.Text) Then
                            Me.Response.Write("<script languaje='javascript'>window.alert('La Modificacion fue Exitosa')</script>")
                            LimpiarFormulario()
                        Else
                            LblError.Visible = True
                            LblError.Text = "Error, no se pudo realizar la Modificacion"
                        End If
                    Else
                        LblError.Visible = True
                        LblError.Text = "Error, el Banco ya existe."
                    End If
                Else
                    LblError.Visible = True
                    LblError.Text = "Error, verifique los datos"
                End If
        
            End If
        Else
            LblError.Visible = True
        End If

    End Sub
    Sub LimpiarFormulario()
        TxtBanco.Text = ""
        HFBanco.Value = ""
        GridBanco.SelectedIndex = -1
        LLenarGridBanco()
        CboImputacion.SelectedIndex = -1
        LblError.Text = ""
        LblError.Visible = False
    End Sub
    Sub LLenarGridBanco()
        ' Dim Cuenta As String
        Dim Denominacion As String
        'Dim Imputacion As String
        'If CboImputacion.SelectedValue.ToString() = -1 Then
        '    Imputacion = ""
        'Else
        '    Imputacion = CboImputacion.SelectedValue.ToString()
        'End If
        If Len(Trim(TxtBanco.Text)) > 0 Then
            Denominacion = Trim(TxtBanco.Text)
        Else
            Denominacion = ""
        End If
        GridBanco.DataSource = Banco.ListarTodos(0, Denominacion, 1)
        GridBanco.DataBind()
    End Sub

    Protected Sub LinkEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkEliminar.Click
        If HFBanco.Value <> "" Then

            If Banco.EliminarBanco(Int(HFBanco.Value)) Then
                Me.Response.Write("<script languaje='javascript'>window.alert('La Eliminacion fue exitosa')</script>")
                LimpiarFormulario()
            Else
                LblError.Visible = True
                LblError.Text = "Error, no se pudo realizar la eliminacion porque hay una Cuenta Relacionada"
            End If
        Else
            Me.Response.Write("<script languaje='javascript'>window.alert('No selecciono Ningun elemento')</script>")
        End If
    End Sub

    Protected Sub LinkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkBuscar.Click
        LLenarGridBanco()
    End Sub

    Protected Sub GridBanco_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridBanco.RowCommand
        If e.CommandName = "Select" Then
            TxtBanco.Text = e.CommandSource.Parent.FindControl("LblDenominacion").text
            HFBanco.Value = e.CommandSource.parent.findcontrol("HFCodigoBanco").value
            CboImputacion.SelectedValue = e.CommandSource.parent.parent.FindControl("HFImputacion").value
        End If
    End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        LimpiarFormulario()
    End Sub

    Protected Sub GridBanco_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridBanco.PageIndexChanging
        GridBanco.PageIndex = e.NewPageIndex
        LLenarGridBanco()
    End Sub
End Class